package com.cci.kangdao.redis;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.cci.kangdao.utilTool.SerializeUtil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.params.SetParams;

@Repository("redisClientUtils")
public class RedisClientUtils {
	private static final Logger log = Logger.getLogger(RedisClientUtils.class);

	@Autowired
	private JedisDataSource redisDataSource;

	public void disconnect() {
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		shardedJedis.disconnect();
	}

	/**
	 * 设置缓存
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @param cacheSeconds
	 *            超时时间，0为不超时
	 * @return
	 */
	public String set(String key, String value, int cacheSeconds) {
		String result = null;

		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		if (shardedJedis == null) {
			return result;
		}
		boolean broken = false;
		try {
			result = shardedJedis.set(key, value);
			if (cacheSeconds != 0) {
				shardedJedis.expire(key, cacheSeconds);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			broken = true;
		} finally {
			redisDataSource.returnResource(shardedJedis, broken);
		}
		return result;
	}

	/**
	 * 设置缓存对象
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @param cacheSeconds
	 *            超时时间，0为不超时
	 * @return
	 */
	public String setObject(String key, Object obj, int cacheSeconds) {
		String result = null;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		if (shardedJedis == null) {
			return result;
		}
		try {
			result = shardedJedis.set(key.getBytes(), SerializeUtil.serialize(obj));
			if (cacheSeconds != 0) {
				shardedJedis.expire(key, cacheSeconds);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 获取单个值
	 * 
	 * @param key
	 * @return
	 */
	public String get(String key) {
		String result = null;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		if (shardedJedis == null) {
			return result;
		}

		boolean broken = false;
		try {
			result = shardedJedis.get(key);
			result = result != null && !"nil".equalsIgnoreCase(result) ? result : null;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			broken = true;
		} finally {
			redisDataSource.returnResource(shardedJedis, broken);
		}
		return result;
	}

	/**
	 * 获取Object对象
	 * 
	 * @param key
	 * @return
	 */
	public Object getObject(String key) {
		Object result = null;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		if (shardedJedis == null) {
			return result;
		}

		try {
			if (shardedJedis.exists(key.getBytes())) {
				byte[] resultByte = shardedJedis.get(key.getBytes());
				result = SerializeUtil.deserialize(resultByte);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 获取List缓存
	 * 
	 * @param key
	 *            键
	 * @return 值
	 */
	public List<String> getList(String key) {
		List<String> value = null;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			if (shardedJedis.exists(key)) {
				value = shardedJedis.lrange(key, 0, -1);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return value;
	}

	/**
	 * 获取List缓存
	 * @param <T>
	 * 
	 * @param key
	 *            键
	 * @return 值
	 */
	public <T> List<T> getObjectList(String key) {
		List<T> value = null;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			if (shardedJedis.exists(key.getBytes())) {
				List<byte[]> list = shardedJedis.lrange(key.getBytes(), 0, -1);
				value = new ArrayList<T>();
				for (byte[] bs : list) {
					value.add((T) SerializeUtil.deserialize(bs));
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return value;
	}

	/**
	 *  获取List缓存
	 * @param key
	 * @param start 0
	 * @param end 第几个
	 * @return
	 */
	public <T> List<T> getObjectList(String key,int start,int end) {
		List<T> value = null;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			if (shardedJedis.exists(key.getBytes())) {
				List<byte[]> list = shardedJedis.lrange(key.getBytes(), start, end);
				value = new ArrayList<T>();
				for (byte[] bs : list) {
					value.add((T) SerializeUtil.deserialize(bs));
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return value;
	}
	/**
	 * 设置List缓存
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @param cacheSeconds
	 *            超时时间，0为不超时
	 * @return
	 */
	public long setList(String key, List<String> value, int cacheSeconds) {
		long result = 0;
		if(value.size()==0){
			return 0;
		}
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			String[] valueParam = new String[value.size()];
			value.toArray(valueParam);
			result = shardedJedis.rpush(key, valueParam);
			if (cacheSeconds != 0) {
				shardedJedis.expire(key, cacheSeconds);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 设置List缓存
	 * @param <T>
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @param cacheSeconds
	 *            超时时间，0为不超时
	 * @return
	 */
	public <T> long setObjectList(String key, List<T> value, int cacheSeconds) {
		long result = 0;
		if(value.size()==0){
			return 0;
		}
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {			
			byte[][] param = new byte[value.size()][];
			for (int i=0; i < value.size(); i++) {
				param[i] = SerializeUtil.serialize(value.get(i));
			}						
			result = shardedJedis.rpush(key.getBytes(), param);
			if (cacheSeconds != 0) {
				shardedJedis.expire(key, cacheSeconds);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 向List缓存中添加值
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @return
	 */
	public long listAdd(String key, String... value) {
		long result = 0;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			result = shardedJedis.rpush(key, value);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 向List缓存中添加值
	 * @param <T>
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @return
	 */
	public <T> long listObjectAdd(String key, T... value) {
		long result = 0;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {			
			byte[][] param = new byte[value.length][];
			for (int i=0; i < value.length; i++) {
				param[i] = SerializeUtil.serialize(value[i]);
			}
			result = shardedJedis.rpush(key.getBytes(), param);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 向List缓存中删除value值
	 * @param <T>
	 *
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @return
	 */
	public <T> long listObjectRemove(String key, T value) {
		long result = 0;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			byte[] param =  SerializeUtil.serialize(value);
			result = shardedJedis.lrem(key.getBytes(), 0,param);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 向List缓存中的头部添加值
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @return
	 */
	public long listAddLeft(String key, String... value) {
		long result = 0;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			result = shardedJedis.lpush(key, value);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 向List缓存中头部添加值
	 * @param <T>
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @return
	 */
	public <T> long listObjectAddLeft(String key, T... value) {
		long result = 0;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			byte[][] param = new byte[value.length][];
			for (int i=0; i < value.length; i++) {
				param[i] = SerializeUtil.serialize(value[i]);
			}
			result = shardedJedis.lpush(key.getBytes(), param);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 获取缓存
	 * 
	 * @param key
	 *            键
	 * @return 值
	 */
	public Set<String> getSet(String key) {
		Set<String> value = null;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			if (shardedJedis.exists(key)) {
				value = shardedJedis.smembers(key);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return value;
	}

	/**
	 * 获取缓存
	 * @param <T>
	 * 
	 * @param key
	 *            键
	 * @return 值
	 */
	public <T> Set<T> getObjectSet(String key) {
		Set<T> value = null;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			if (shardedJedis.exists(key.getBytes())) {
				value = new HashSet<T>();
				Set<byte[]> set = shardedJedis.smembers(key.getBytes());
				for (byte[] bs : set) {
					value.add((T) SerializeUtil.deserialize(bs));
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return value;
	}

	/**
	 * 设置Set缓存
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @param cacheSeconds
	 *            超时时间，0为不超时
	 * @return
	 */
	public long setSet(String key, Set<String> value, int cacheSeconds) {
		long result = 0;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			String[] param = new String[value.size()];
			value.toArray(param);
			result = shardedJedis.sadd(key, param);
			if (cacheSeconds != 0) {
				shardedJedis.expire(key, cacheSeconds);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 设置Set缓存
	 * @param <T>
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @param cacheSeconds
	 *            超时时间，0为不超时
	 * @return
	 */
	public <T> long setObjectSet(String key, Set<T> value, int cacheSeconds) {
		long result = 0;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
//			Set<byte[]> set = new HashSet();
//			for (Object o : value) {
//				set.add(SerializeUtil.serialize(o));
//			}
//			result = shardedJedis.sadd(key.getBytes(), (byte[][]) set.toArray());
			
			byte[][] set = new byte[value.size()][];
			int i=0;
			for (Object o : value) {
				set[i] =SerializeUtil.serialize(o);
				i++;
			}
			result = shardedJedis.sadd(key.getBytes(), set);
			if (cacheSeconds != 0) {
				shardedJedis.expire(key, cacheSeconds);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 向Set缓存中添加值
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @return
	 */
	public long setSetAdd(String key, String... value) {
		long result = 0;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			result = shardedJedis.sadd(key, value);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 向Set缓存中添加值
	 * @param <T>
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @return
	 */
	public <T> long setSetObjectAdd(String key, T... value) {
		long result = 0;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
//			Set<byte[]> set = new HashSet();
//			for (Object o : value) {
//				set.add(SerializeUtil.serialize(o));
//			}
//			result = shardedJedis.sadd(key.getBytes(), (byte[][]) set.toArray());
			
			byte[][] set = new byte[value.length][];
			int i=0;
			for (Object o : value) {
				set[i] =SerializeUtil.serialize(o);
				i++;
			}
			result = shardedJedis.sadd(key.getBytes(), set);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 获取Map缓存
	 * 
	 * @param key
	 *            键
	 * @return 值
	 */
	public Map<String, String> getMap(String key) {
		Map<String, String> value = null;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			if (shardedJedis.exists(key)) {
				value = shardedJedis.hgetAll(key);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return value;
	}

	/**
	 * 获取Map缓存
	 * @param <T>
	 * 
	 * @param key
	 *            键
	 * @return 值
	 */
	public <T> Map<String, T> getObjectMap(String key) {
		Map<String, T> value = null;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			if (shardedJedis.exists(key.getBytes())) {
				value = new HashMap<String, T>();
				Map<byte[], byte[]> map = shardedJedis.hgetAll(key.getBytes());
				for (Map.Entry<byte[], byte[]> e : map.entrySet()) {
					value.put(new String(e.getKey()), (T) SerializeUtil.deserialize(e.getValue()));
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return value;
	}

	/**
	 * 设置Map缓存
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @param cacheSeconds
	 *            超时时间，0为不超时
	 * @return
	 */
	public String setMap(String key, Map<String, String> value, int cacheSeconds) {
		String result = null;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			result = shardedJedis.hmset(key, value);
			if (cacheSeconds != 0) {
				shardedJedis.expire(key, cacheSeconds);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 设置Map缓存
	 * @param <T>
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @param cacheSeconds
	 *            超时时间，0为不超时
	 * @return
	 */
	public <T> String setObjectMap(String key, Map<String, T> value, int cacheSeconds) {
		String result = null;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			Map<byte[], byte[]> map = new HashMap<byte[], byte[]>();
			for (Entry<String, T> e : value.entrySet()) {
				map.put(e.getKey().getBytes(), SerializeUtil.serialize(e.getValue()));
			}
			result = shardedJedis.hmset(key.getBytes(), map);
			if (cacheSeconds != 0) {
				shardedJedis.expire(key, cacheSeconds);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	public <T> String setObjectMap(String key, Map<String, T> value) {
		String result = null;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			Map<byte[], byte[]> map = new HashMap<byte[], byte[]>();
			for (Entry<String, T> e : value.entrySet()) {
				map.put(e.getKey().getBytes(), SerializeUtil.serialize(e.getValue()));
			}
			result = shardedJedis.hmset(key.getBytes(), map);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 向Map缓存中添加值
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @return
	 */
	public String mapPut(String key, Map<String, String> value) {
		String result = null;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			result = shardedJedis.hmset(key, value);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 向Map缓存中添加值
	 * @param <T>
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @return
	 */
	public <T> String mapObjectPut(String key, Map<String, T> value) {
		String result = null;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			Map<byte[], byte[]> map = new HashMap<byte[], byte[]>();
			for (Entry<String, T> e : value.entrySet()) {
				map.put(e.getKey().getBytes(), SerializeUtil.serialize(e.getValue()));
			}
			result = shardedJedis.hmset(key.getBytes(), map);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 移除Map缓存中的值
	 * @return
	 */
	public long mapRemove(String key, String mapKey) {
		long result = 0;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			result = shardedJedis.hdel(key, mapKey);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 移除Map缓存中的值
	 * @return
	 */
	public long mapObjectRemove(String key, String mapKey) {
		long result = 0;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			result = shardedJedis.hdel(key.getBytes(), mapKey.getBytes());
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 判断Map缓存中的Key是否存在
	 * @return
	 */
	public boolean mapExists(String key, String mapKey) {
		boolean result = false;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			result = shardedJedis.hexists(key, mapKey);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 判断Map缓存中的Key是否存在
	 * @return
	 */
	public boolean mapObjectExists(String key, String mapKey) {
		boolean result = false;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			result = shardedJedis.hexists(key.getBytes(), mapKey.getBytes());
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 查询key是否存在
	 * 
	 * @param key
	 * @return
	 */
	public Boolean exists(String key) {
		Boolean result = false;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		if (shardedJedis == null) {
			return result;
		}
		try {
			result = shardedJedis.exists(key);
			if (!result) {
				shardedJedis.exists(key.getBytes());
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 查询key的类型
	 * 
	 * @param key
	 * @return
	 */
	public String type(String key) {
		String result = null;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		if (shardedJedis == null) {
			return result;
		}
		try {
			result = shardedJedis.type(key);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return result;
	}

	/**
	 * 删除key
	 * 
	 * @param key
	 * @return
	 */
	public Boolean delkey(String key) {
		Boolean result = false;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		if (shardedJedis == null) {
			return result;
		}
		boolean broken = false;
		try {
			if (shardedJedis.exists(key)) {
				shardedJedis.del(key);
				result = true;
			}
			if (shardedJedis.exists(key.getBytes())) {
				shardedJedis.del(key.getBytes());
				result = true;
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			broken = true;
		} finally {
			redisDataSource.returnResource(shardedJedis, broken);
		}
		return result;
	}
	
	/**
	 * 删除所有的key
	 * @return
	 */
	public Boolean delAllkey() {
		Boolean result = false;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		if (shardedJedis == null) {
			return result;
		}		
		boolean broken = false;
		try {
			shardedJedis.getShard("1").flushAll();
			shardedJedis.getShard("1").flushDB();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			broken = true;
		} finally {
			redisDataSource.returnResource(shardedJedis, broken);
		}
		return result;
	}
	
	public long hset(String key, String field, String value) {
		long result = 0;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		boolean broken = false;
		try{
			result = shardedJedis.hset(key, field, value);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			broken = true;
		} finally {
			redisDataSource.returnResource(shardedJedis, broken);
		}
		return result;
	}

	public String hget(String key, String field) {
		String result = "";
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		boolean broken = false;
		try{
			result = shardedJedis.hget(key, field);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			broken = true;
		} finally {
			redisDataSource.returnResource(shardedJedis, broken);
		}
		return result;
	}
	
	public long hdel(String key, String... fields) {
		long result = 0;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		boolean broken = false;
		try{
			result = shardedJedis.hdel(key, fields);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			broken = true;
		} finally {
			redisDataSource.returnResource(shardedJedis, broken);
		}
		return result;
	}

	public Boolean lock(String key,Long seconds) {
		Boolean lock = false;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		if (shardedJedis == null) {
			return lock;
		}
		try {
			SetParams setParams = new SetParams();
			setParams.nx();
			setParams.ex(seconds.intValue());
			String result = shardedJedis.set(key, "lock", setParams);
			lock = "OK".equalsIgnoreCase(result);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return lock;
	}

    /**
     *
     * @param key 递增数据key
     * @param expire 失效秒数，0为永不失效。
     * @return 递增整数
     */
	public long incr(String key,int expire) {
		long result = 0L;
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		boolean broken = false;
		try{
			result = shardedJedis.incr(key);
			if(result != 0L && expire != 0){
                shardedJedis.expire(key,expire);
            }
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			broken = true;
		} finally {
			redisDataSource.returnResource(shardedJedis, broken);
		}
		return result;
	}


	/**
	 * 模糊查询符合正则的key值列表
	 * @param pattern 正则条件 类似于Common*
	 * @return
	 */
	public List<String> keys(String pattern) {
		List<String> keyList = new ArrayList<>();
		ShardedJedis shardedJedis = redisDataSource.getRedisClient();
		try {
			Collection<Jedis> allShards = shardedJedis.getAllShards();
			for(Jedis jedis:allShards){
				Set<String> keys = jedis.keys(pattern);
				keyList.addAll(keys);
			}
			return keyList;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			redisDataSource.returnResource(shardedJedis);
		}
		return keyList;
	}
}
